package cc.lzmsh.ruuner;

import cc.lzmsh.domain.entity.Article;
import cc.lzmsh.mapper.ArticleMapper;
import cc.lzmsh.utils.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Author:   msh
 * Date:     2024/9/24 下午2:05
 * Description:
 */

@Component
public class ViewCountRunner implements CommandLineRunner {
    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    private RedisCache redisCache;

    @Override
    public void run(String... args) throws Exception {
        // 查询文章信息,id 和 viewCount
        List<Article> articles = articleMapper.selectList(null);
        Map<String, Integer> viewCountMap = articles.stream().collect(Collectors.
                toMap(article -> article.getId().toString(), article -> article.getViewCount().intValue()));
        // 更新到redis中
        redisCache.setCacheMap("article:viewCount", viewCountMap);
    }
}
